<template>
  <el-dialog
      :title="config.title"
      :visible="visible"
      :close-on-click-modal="false"
      v-el-drag-dialog
      @close="onClose"
      @open="onOpen"
      width="400px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="登录账号" v-if="!row" prop="username" :rules="[{ required: !row, message: '请输入用户登录账号', trigger: 'change' }]">
        <el-input v-model="form.username"/>
      </el-form-item>
      <el-form-item label="员工名称" prop="nickname">
        <el-input v-model="form.nickname"/>
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input v-model="form.mobile"/>
      </el-form-item>
      <el-form-item label="登录密码" prop="password" :rules="[{ required: !row, message: '请输入用户密码', trigger: 'change' }]">
        <el-input v-model="form.password"/>
        <div class="tip" v-if="row">如需修改密码，请输入</div>
      </el-form-item>
      <el-form-item label="角色" prop="roleIdList">
        <el-select v-model="form.roleIdList" multiple clearable>
          <el-option
              v-for="item in roleList" :key="item.value"
              :label="item.name" :value="item.id"
              :disabled="item.isDisabled"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import staffApi from '@/api/staff'
  import sysRoleApi from '@/api/sysRole'
  import { validPhone } from '@/utils/validate'

  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      row: {
        type: Object,
        default: function() {
          return null
        }
      },
      callback: {
        type: Function,
        default: function() {
          if (this.$parent.$refs.dataTable) {
            this.$parent.$refs.dataTable.getDataList()
          }
        }
      }
    },
    data() {
      return {
        form: {
          username:'',
          nickname:'',
          mobile: '',
          password: '',
          roleIdList: []
        },
        rules: {
          nickname: [{ required: true, trigger: 'change', message: '请输入员工名称' }],
          mobile: [{
            required: true, trigger: 'change',
            validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入手机号码'))
              } else if (!validPhone(value)) {
                callback(new Error('请输入正确的手机号码'))
              } else {
                callback()
              }
            }
          }],
          roleIdList: [{ required: true, trigger: 'change', message: '请选择角色' }]
        },
        roleList: [],

        config: {},
        configMap: {
          add: { api: 'add', title: '添加员工' },
          edit: { api: 'update', title: '修改员工' }
        }
      }
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let nickname = this.form.nickname
            staffApi[this.config.api]({ ...this.form, nickname: nickname, realName: nickname }).then(res => {
              this.callback()
              this.onClose()
              this.$notify({
                title: '成功',
                message: this.config.title + '成功',
                type: 'success'
              })
            })
          }
        })
      },
      getData() {
        staffApi.get(this.row.id).then(res => {
          let { id, mobile, username,nickname, roleIdList } = res.data
          this.form.id = id
          this.form.mobile = mobile
          this.form.username = username
          this.form.roleIdList = roleIdList
          this.form.nickname = nickname
        })
      },
      onOpen() {
        sysRoleApi.listAll().then(res => {
          this.roleList = res.data
        })

        if (this.row) {
          this.config = this.configMap['edit']
          this.$nextTick(() => {
            this.getData()
          })
        } else {
          this.config = this.configMap['add']
          this.form.username = ''
        }
      },
      onClose() {
        this.$refs.form.resetFields()
        this.$emit('update:visible', false)
      }
    },
    mounted() {
    }
  }
</script>

<style scoped>

</style>
